package com.example.demo01.demo;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.FragmentTransaction;

import com.example.demo01.R;
import com.example.demo01.consts.Setting;
import com.example.demo01.fragments.TitleFragment;
import com.example.demo01.utils.Utils;

public class PreferenceDemo extends AppCompatActivity {
    private Utils utils = new Utils(this);

    private EditText ev_preferences_name, ev_preferences_nickname;
    private Button btn_preference_write, btn_preference_read;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_preference);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        //. 添加标题
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.fl_title, TitleFragment.newInstance("Preference示例"))
                .commit();

        initView();
    }

    //. 初始化视图
    private void initView() {
        ev_preferences_name = findViewById(R.id.ev_preferences_name);
        ev_preferences_nickname = findViewById(R.id.ev_preferences_nickname);
        btn_preference_write = findViewById(R.id.btn_preference_write);
        btn_preference_read = findViewById(R.id.btn_preference_read);

        addListener();
    }

    //. 添加点击事件
    private void addListener() {
        btn_preference_write.setOnClickListener(v -> {
            //. 获取数据
            String name = ev_preferences_name.getText().toString();
            String nickname = ev_preferences_nickname.getText().toString();
            //. 校验数据
            if (name.isEmpty() || nickname.isEmpty()) {
                utils.showToast("信息填写不完整");
                return;
            }
            //. 储存数据
            //？ getSharedPreferences()可全局共享数据
            //？ getPreferences()可单Activity独享数据
            //？ MODE_PRIVATE 表示私有数据与当前应用共享
            SharedPreferences preferences = this.getSharedPreferences(Setting.PREFERENCE_KEY, MODE_PRIVATE);
            preferences.edit().putString(Setting.PREFERENCE_KEY_NAME, name).apply();
            preferences.edit().putString(Setting.PREFERENCE_KEY_NICKNAME, nickname).apply();

            //. 提示用户
            utils.showToast("数据储存成功");
        });

        btn_preference_read.setOnClickListener(v -> {
            //. 读取数据
            SharedPreferences preferences = this.getSharedPreferences(Setting.PREFERENCE_KEY, MODE_PRIVATE);
            String name = preferences.getString(Setting.PREFERENCE_KEY_NAME, "");
            String nickname = preferences.getString(Setting.PREFERENCE_KEY_NICKNAME, "");
            //. 校验数据
            if (name.isEmpty() || nickname.isEmpty()) {
                utils.showToast("数据读取失败");
                return;
            }
            //. 显示数据
            ev_preferences_name.setText(name);
            ev_preferences_nickname.setText(nickname);
            //. 提示用户
            utils.showToast("数据读取成功");
        });

    }
}